package edu.uw.cs.cse461.sp12.SNet;

import java.io.IOException;
import java.util.Properties;

import org.json.JSONException;

import edu.uw.cs.cse461.sp12.OS.DDNSResolverService;
import edu.uw.cs.cse461.sp12.OS.OS;
import edu.uw.cs.cse461.sp12.OS.SNetController;
import edu.uw.cs.cse461.sp12.OS.exceptions.DDNSAuthorizationException;
import edu.uw.cs.cse461.sp12.OS.exceptions.DDNSNoAddressException;
import edu.uw.cs.cse461.sp12.OS.exceptions.DDNSNoSuchNameException;
import edu.uw.cs.cse461.sp12.OS.exceptions.DDNSRuntimeException;
import edu.uw.cs.cse461.sp12.OS.exceptions.DDNSTTLExpiredException;
import edu.uw.cs.cse461.sp12.OS.exceptions.DDNSZoneException;
import edu.uw.cs.cse461.sp12.androidSNet.R;
import edu.uw.cs.cse461.sp12.util.DB461.DB461Exception;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class UpdateData extends Activity {
	
	Button friendButton;
	Button unfriendButton;
	Spinner members;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.updatedata);
        friendButton = (Button)findViewById(R.id.friendButton);
        unfriendButton = (Button)findViewById(R.id.unfriendButton);
        members = (Spinner)findViewById(R.id.members);
        try {
			String[] memberList = ((SNetController)OS.getRPCService("snet")).getMembers();
			System.out.println(memberList.length + "memberlist"); 
			for (int i = 0; i < memberList.length; i++) {
				if(memberList[i] == null) {
					memberList[i] = "";
				}
				System.out.println(memberList[i]);
			}
			ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
			for (int i = 0; i < memberList.length; i++) {
				adapter.add(memberList[i]);
			}
			members.setAdapter(adapter);
		} catch (DB461Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }
    
    public void friend(View view) {
    	String value = (String)members.getSelectedItem();
    	try {
			((SNetController)OS.getRPCService("snet")).addFriend(value);
		} catch (DB461Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void unfriend(View view) {
    	String value = (String)members.getSelectedItem();
    	try {
			((SNetController)OS.getRPCService("snet")).removeFriend(value);
		} catch (DB461Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void contact(View view) {
    	//String value = (String)members.getSelectedItem();
    	new Thread(){
            public void run() {
              // worker thread starts running asynchronously here
            	try {
            		String value = (String)members.getSelectedItem();
        			((SNetController)OS.getRPCService("snet")).contactMember(value);
        		} catch (Exception e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        		}
            }
          }.start();
    	 
    }
    
    public void fixdb(View view) {
    	String value = (String)members.getSelectedItem();
    	try {
			((SNetController)OS.getRPCService("snet")).fixDB();
		} catch (DB461Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
